import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class C {
    public static void main(String[] args) {
        Data3 data = new Data3();
 
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data.printA();
            }
        },"A").start();
        
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data.printB();
            }
        },"B").start();
        
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data.printC();
            }
        },"C").start();
    }
}
 
//资源类
class Data3{
    //创建Lock锁
   private Lock lock = new ReentrantLock();
    //同步监视器
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();
    private int number = 1;//1就是A执行,2就是B执行
 
   public void printA(){
        lock.lock();
       try {
           while(number != 1){
               //等待
               condition1.await();
           }
           System.out.println(Thread.currentThread().getName()+"===>AAAAAAAAA");
           //唤醒指定线程
           number = 2;
           condition2.signal();
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           lock.unlock();
       }
   }
 
    public void printB(){
        lock.lock();
        try {
            while(number != 2){
                //等待
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"===>BBBBBBBB");
            //唤醒指定线程
            number = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
 
    public void printC(){
        lock.lock();
        try {
            while(number != 3){
                //等待
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"===>CCCCCCCCC");
            //唤醒指定线程
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}